const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  Laboratory,
} = require('../db/model');

class LaboratoryService extends Service {
  async getAllLaboratory() {
    const res = await Laboratory.findAll().catch(err => {
      console.log(`Laboratory 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`Laboratory 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getLaboratory(laboratory) {
    if (!laboratory?.lId) {
      console.log(`Laboratory 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${laboratory}`, laboratory);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    const res = await Laboratory.findOne({
      where: {
        lId: laboratory?.lId || -1
      },
    }).catch(err => {
      console.log(`Laboratory 表查找失败: ${err}\n参数: ${laboratory}`, laboratory);
    });
    if (res === undefined) {
      console.log(`Laboratory 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${laboratory}`, laboratory);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`Laboratory 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${laboratory}`, laboratory);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendLaboratory(laboratory) {
    let res = await this.getLaboratory(laboratory);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`Laboratory 表新增失败: ${res}\n参数: ${laboratory}`, laboratory);
      return res;
    }
    res = await Laboratory.create(laboratory).catch(err => {
      console.log(`Laboratory 表新增失败: ${err}\n参数: ${laboratory}`, laboratory);
    });
    if (res === undefined) {
      console.log(`Laboratory 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${laboratory}`, laboratory);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateLaboratory(laboratory) {
    let res = await this.getLaboratory(laboratory);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Laboratory 表更新失败: ${res}\n参数: ${laboratory}`, laboratory);
      return res;
    }
    res = await Laboratory.update(laboratory, {
      where: {
        lId: laboratory?.lId || -1
      },
    }).catch(err => {
      console.log(`Laboratory 表更新失败: ${err}\n参数: ${laboratory}`, laboratory);
    });
    if (res === undefined) {
      console.log(`Laboratory 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${laboratory}`, laboratory);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyLaboratory(laboratory) {
    let res = await this.getLaboratory(laboratory);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`Laboratory 表删除失败: ${res}\n参数: ${laboratory}`, laboratory);
      return res;
    }
    res = await Laboratory.destroy({
      where: {
        lId: laboratory?.lId || -1
      },
    }).catch(err => {
      console.log(`Laboratory 表删除失败: ${err}\n参数: ${laboratory}`, laboratory);
    });
    if (res === undefined) {
      console.log(`Laboratory 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${laboratory}`, laboratory);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = LaboratoryService;
